﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Net;

namespace aucn.web.au
{
    public partial class VisitRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.Attributes["submenu"] = "Services";
            this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                this.SendMail(txtParentName.Text, txtParentTel.Text, txtAddr.Text, txtStudentName.Text, txtSchool.Text, txtContent.Text);
            }
        }

        protected void SendMail(string pName, string pTel, string pAddr, string sName, string school, string content)
        {


            string[] lines = Regex.Split(ConfigurationManager.AppSettings["VisitMailBodyFormat"], @"\r\n");
            StringBuilder mailBody = new StringBuilder();
            foreach (string s in lines)
            {
                string t = s.Trim();
                if (t.Length > 0)
                {
                    mailBody.AppendLine(t);
                }
            }

            string body = mailBody.ToString()
                .Replace("%pname%", pName)
                .Replace("%tel%", pTel)
                .Replace("%addr%", pAddr)
                .Replace("%sname%", sName)
                .Replace("%school%", school)
                .Replace("%content%", content);

            MailMessage mm = new MailMessage();
            mm.To.Add(ConfigurationManager.AppSettings["MailAddress"]);
            mm.From = new MailAddress("au.sh.china@gmail.com", "网站邮件");
            mm.Subject = ConfigurationManager.AppSettings["VisitMailSubjectFormat"];
            mm.BodyEncoding = System.Text.Encoding.UTF8;
            //mm.ReplyTo = new MailAddress(mailAddress, pName);
            mm.SubjectEncoding = System.Text.Encoding.UTF8;
            mm.Body = body;
            mm.IsBodyHtml = false;

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            //client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //var client = new SmtpClient();
            //var client = new SmtpClient();
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("au.sh.china@gmail.com", "a12345678a");
            client.EnableSsl = true;
            
            //client.
            try
            {
                client.Send(mm);
                MultiView1.ActiveViewIndex = 1;
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
                MultiView1.ActiveViewIndex = 2;
            }
        }
    }
}

